package com.cr.front.news.controller;

import com.cr.common.response.JsonResult;
import com.cr.common.response.PageData;
import com.cr.front.news.pojo.query.SearchParam;
import com.cr.front.news.pojo.vo.*;
import com.cr.front.news.service.NewsService;
import com.cr.front.news.service.es.ESSearchRun;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/news")
@Api(tags = "资讯管理")
public class NewsController {

    @Autowired
    private NewsService newsService;

    @Autowired
    private ESSearchRun searchRun;

    @GetMapping("/latestNews")
    @ApiOperation("获取最新资讯")
    public JsonResult getNews() {
        List<NewsByTimeVO> list = newsService.getLatestNews();
        return JsonResult.success(list);
    }


    @GetMapping("/searchBy")
    @ApiOperation("全部资讯")
    public JsonResult getTotalNewsByCategoryId(SearchParam searchParam) throws IOException {

        List<NewsESVO> listData = searchRun.searchByMatch(searchParam);

        long total = searchRun.getTotalHits();

        PageData<NewsESVO> pageData = new PageData<>(total, listData);

        return JsonResult.success(pageData);
    }


    @GetMapping("/byNewsId/{id}")
    @ApiOperation("获取文章详情")
    public JsonResult getNewsById(@PathVariable Integer id) {
        NewsVO newsVO = newsService.getDetailNews(id);
        return JsonResult.success(newsVO);
    }


    @GetMapping("/hot")
    @ApiOperation("获取最热资讯")
    public JsonResult getNewsByViews() {
        List<NewsByViewsVO> list = newsService.getNewsByViews();
        return JsonResult.success(list);
    }
}

















